'use strict';

const Controller = require('egg').Controller;
const jwt = require('jsonwebtoken');

class UserController extends Controller {
    //登录
    async login() {
        let {ctx,service} = this;
        let {username,password} = ctx.request.body;
        if(username && /^[0-9a-zA-Z]{4,6}$/.test(password)){
            //加密
            let hmcPwd = ctx.helper.hmc(password);
            let target = await service.user.login(username,hmcPwd);
            console.log(target);//[{role_id}]
            if(target.length){
                let token = jwt.sign({role_id:target[0].role_id},'1801a',{expiresIn:60*60*5});
                ctx.body = {
                    code:1,
                    msg:'登录成功',
                    token
                }
            }else{
                ctx.body = {
                    code:0,
                    msg:'登录失败'
                }
            }
        }else{
            ctx.body = {
                code:0,
                msg:'参数有误'
            }
        }
    }

    //请求菜单
    async list(){
        let {ctx} = this;
        ctx.body = [1]
    }
}

module.exports = UserController;
